﻿using Product.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UboxUI;

namespace Product
{
    public partial class FrmProductProperty : FrmFather
    {
        public FrmProductProperty()
        {
            InitializeComponent();
            this.BtnClose.Left = this.Width - this.BtnClose.Width - 10;
            this.BtnNarrow.Left = this.BtnClose.Left - this.BtnNarrow.Width - 10;
            this.panel1.Top = 0;
            this.panel2.Top = this.panel1.Height;

        }

        string conn = Settings.Default.strConn;
        string Property;
        private void FrmProductProperty_Load(object sender, EventArgs e)
        {
            this.cboProperty.SelectedIndexChanged += cboProperty_SelectedIndexChanged;
            this.cboProperty.SelectedIndex = 0;
            LoadDB();
        }

        UBOXDataSet ds = new UBOXDataSet();
        private void LoadDB()
        {
            this.brandTableAdapter1.Fill(ds.brand);
            this.categoryTableAdapter1.Fill(ds.category);
            this.production_romTableAdapter1.Fill(ds.production_rom);
            this.production_ramTableAdapter1.Fill(ds.production_ram);
            this.production_operating_systemTableAdapter1.Fill(ds.production_operating_system);
            this.production_connection_standardTableAdapter1.Fill(ds.production_connection_standard);
            this.production_processor_coreTableAdapter1.Fill(ds.production_processor_core);
            this.production_processor_speedTableAdapter1.Fill(ds.production_processor_speed);
            this.production_screen_sizeTableAdapter1.Fill(ds.production_screen_size);
            this.production_srceen_resolutionTableAdapter1.Fill(ds.production_srceen_resolution);
            this.production_cameraTableAdapter1.Fill(ds.production_camera);
            this.production_functionTableAdapter1.Fill(ds.production_function);
        }

        void cboProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            Property = this.cboProperty.Text;
            switch (Property)
            {
                case "類別":
                    this.dataGridView.DataSource = ds.category;
                    break;
                case "品牌":
                    this.dataGridView.DataSource = ds.brand;
                    break;
                case "容量":
                    this.dataGridView.DataSource = ds.production_rom;
                    break;
                case "記憶體":
                    this.dataGridView.DataSource = ds.production_ram;
                    break;
                case "OS":
                    this.dataGridView.DataSource = ds.production_operating_system;
                    break;
                case "通訊標準":
                    this.dataGridView.DataSource = ds.production_connection_standard;
                    break;
                case "處理器":
                    this.dataGridView.DataSource = ds.production_processor_core;
                    break;
                case "速度":
                    this.dataGridView.DataSource = ds.production_processor_speed;
                    break;
                case "螢幕尺寸":
                    this.dataGridView.DataSource = ds.production_screen_size;
                    break;
                case "螢幕像素":
                    this.dataGridView.DataSource = ds.production_srceen_resolution;
                    break;
                case "相機畫素":
                    this.dataGridView.DataSource = ds.production_camera;
                    break;
                case "其他功能":
                    this.dataGridView.DataSource = ds.production_function;
                    break;
            }
            this.dataGridView.Columns[0].HeaderText = "編號";
            this.dataGridView.Columns[1].HeaderText = "項目";
            this.dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

       

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmInsert f = new FrmInsert();
            f.Text = string.Format("{0}", this.cboProperty.Text);
            f.ShowDialog();
            string newItem = f.txtDescription.Text;

            UboxDataClassesDataContext db = new UboxDataClassesDataContext(conn);
            if (f.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                #region switch
                switch (Property)
                {
                    case "類別":
                        db.category.InsertOnSubmit(new category { category_name = newItem });
                        db.SubmitChanges();
                        break;
                    case "品牌":
                        db.brand.InsertOnSubmit(new brand { brand_name = newItem });
                        db.SubmitChanges();
                        break;
                    case "容量":
                        short s;
                        if (short.TryParse(newItem, out s))
                        {
                            db.production_rom.InsertOnSubmit(new production_rom { rom = s });
                            db.SubmitChanges();
                        }
                        else
                        {
                            MessageBox.Show("請輸入正確的資訊");
                        }
                        break;
                    case "記憶體":
                        byte b;
                        if (byte.TryParse(newItem, out b))
                        {
                            db.production_ram.InsertOnSubmit(new production_ram { ram = b });
                            db.SubmitChanges();
                        }
                        else
                        {
                            MessageBox.Show("請輸入正確的資訊");
                        }
                        break;
                    case "OS":
                        db.production_operating_system.InsertOnSubmit(new production_operating_system { operating_system = newItem });
                        db.SubmitChanges();
                        break;
                    case "通訊標準":
                        db.production_connection_standard.InsertOnSubmit(new production_connection_standard { connection_standard = newItem });
                        db.SubmitChanges();
                        break;
                    case "處理器":
                        db.production_processor_core.InsertOnSubmit(new production_processor_core { processor_core = newItem });
                        db.SubmitChanges();
                        break;
                    case "速度":
                        decimal d;
                        if (decimal.TryParse(newItem, out d))
                        {
                            db.production_processor_speed.InsertOnSubmit(new production_processor_speed { processor_speed = d });
                            db.SubmitChanges();
                        }
                        else
                        {
                            MessageBox.Show("請輸入正確的資訊");
                        }
                        break;
                    case "螢幕尺寸":
                        decimal d1;
                        if (decimal.TryParse(newItem, out d1))
                        {
                            db.production_screen_size.InsertOnSubmit(new production_screen_size { screen_size = d1 });
                            db.SubmitChanges();
                        }
                        else
                        {
                            MessageBox.Show("請輸入正確的資訊");
                        }
                        break;
                    case "螢幕像素":
                        db.production_srceen_resolution.InsertOnSubmit(new production_srceen_resolution { screen_resolution = newItem });
                        db.SubmitChanges();
                        break;
                    case "相機畫素":
                        short s1;
                        if (short.TryParse(newItem, out s1))
                        {
                            db.production_camera.InsertOnSubmit(new production_camera { camera_resolution = s1 });
                            db.SubmitChanges();
                        }
                        else
                        {
                            MessageBox.Show("請輸入正確的資訊");
                        }
                        break;
                    case "其他功能":
                        db.production_function.InsertOnSubmit(new production_function { function = newItem });
                        db.SubmitChanges();
                        break;
                }
                #endregion
                LoadDB();
                cboProperty_SelectedIndexChanged(sender, e);               
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ds.HasChanges())
            {

                UBOXDataSet dsChanged = (UBOXDataSet)ds.GetChanges(DataRowState.Deleted | DataRowState.Modified);
                try
                {
                    brandTableAdapter1.Update(dsChanged);
                    categoryTableAdapter1.Update(dsChanged);                
                    production_cameraTableAdapter1.Update(dsChanged);
                    production_connection_standardTableAdapter1.Update(dsChanged);
                    production_operating_systemTableAdapter1.Update(dsChanged);
                    production_processor_coreTableAdapter1.Update(dsChanged);
                    production_processor_speedTableAdapter1.Update(dsChanged);
                    production_ramTableAdapter1.Update(dsChanged);
                    production_romTableAdapter1.Update(dsChanged);
                    production_srceen_resolutionTableAdapter1.Update(dsChanged);
                    production_screen_sizeTableAdapter1.Update(dsChanged);
                    production_functionTableAdapter1.Update(dsChanged);
                    ds.AcceptChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                MessageBox.Show("儲存成功");

                cboProperty_SelectedIndexChanged(sender, e);
            }
            else
            {
                MessageBox.Show("沒有新的更新紀錄");
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        #region LINQtoSQL項目 (舊版)
        /*           
        void cboProperty_SelectedIndexChanged(object sender, EventArgs e)
        {

            UboxDataClassesDataContext db = new UboxDataClassesDataContext(conn);
            Property = this.cboProperty.Text;
            switch (Property)
            {
                case "類別":
                    var q1 = from a in db.category
                             select a;
                    this.dataGridView.DataSource = q1.ToList();
                    break;
                case "品牌":
                    var q2 = from a in db.brand
                             select a;
                    this.dataGridView.DataSource = q2.ToList();
                    break;
                case "容量":
                    var q3 = from a in db.production_rom
                             select a;
                    this.dataGridView.DataSource = q3.ToList();
                    break;
                case "記憶體":
                    var q4 = from a in db.production_ram
                             select a;
                    this.dataGridView.DataSource = q4.ToList();
                    break;
                case "OS":
                    var q5 = from a in db.production_operating_system
                             select a;
                    this.dataGridView.DataSource = q5.ToList();
                    break;
                case "通訊標準":
                    var q6 = from a in db.production_connection_standard
                             select a;
                    this.dataGridView.DataSource = q6.ToList();
                    break;
                case "處理器":
                    var q7 = from a in db.production_processor_core
                             select a;
                    this.dataGridView.DataSource = q7.ToList();
                    break;
                case "速度":
                    var q8 = from a in db.production_processor_speed
                             select a;
                    this.dataGridView.DataSource = q8.ToList();
                    break;
                case "螢幕尺寸":
                    var q9 = from a in db.production_screen_size
                             select a;
                    this.dataGridView.DataSource = q9.ToList();
                    break;
                case "螢幕像素":
                    var q10 = from a in db.production_srceen_resolution
                              select a;
                    this.dataGridView.DataSource = q10.ToList();
                    break;
                case "相機畫素":
                    var q11 = from a in db.production_camera
                              select a;
                    this.dataGridView.DataSource = q11.ToList();
                    break;
                case "其他功能":
                    var q12 = from a in db.production_function
                              select a;
                    this.dataGridView.DataSource = q12.ToList();
                    break;

            }
            this.dataGridView.Columns[0].HeaderText = "編號";
            this.dataGridView.Columns[1].HeaderText = "項目";
            this.dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

        }
        
                #endregion
                cboProperty_SelectedIndexChanged(sender, e);
            }
        }
                     */
        #endregion

        //private void btnAdd_Click(object sender, EventArgs e) 
        //{
        //    FrmInsert f = new FrmInsert();
        //    f.Text = this.cboProperty.Text;
        //    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //    {
        //        DataRow MyDataRow = MyDataTable.NewRow();
        //        MyDataRow[1] = f.txtDescription.Text;
        //        MyDataTable.Rows.Add(MyDataRow);
        //    }
        //    f.Dispose();
        //}




        #region 按鈕樣式

        private void btnSave_MouseEnter(object sender, EventArgs e)
        {
            this.btnSave.FlatAppearance.BorderSize = 0;
            this.btnSave.ForeColor = Color.White;
        }

        private void btnSave_MouseLeave(object sender, EventArgs e)
        {
            this.btnSave.FlatAppearance.BorderSize = 1;
            this.btnSave.ForeColor = Color.Black;
        }

        private void btnCancel_MouseEnter(object sender, EventArgs e)
        {
            this.btnCancel.FlatAppearance.BorderSize = 0;
            this.btnCancel.ForeColor = Color.White;
        }

        private void btnCancel_MouseLeave(object sender, EventArgs e)
        {
            this.btnCancel.FlatAppearance.BorderSize = 1;
            this.btnCancel.ForeColor = Color.Black;
        }

        #endregion


    }
}
